package com.cenntro.common.mongdb;
//package com.cenntro.auth.mongodb;
//
//
//import java.net.UnknownHostException;
//import java.util.Collections;
//
//import org.bson.Document;
//
//import com.mongodb.MongoClient;
//import com.mongodb.MongoClientOptions;
//import com.mongodb.MongoCredential;
//import com.mongodb.ServerAddress;
//import com.mongodb.client.MongoCollection;
//import com.mongodb.client.MongoDatabase;
//
//import ch.qos.logback.core.UnsynchronizedAppenderBase;
//
///**
// * MongoDBAppender适配类
// */
//public abstract class MongoDBAppenderBase<E> extends UnsynchronizedAppenderBase<E> {
// 
//	private MongoClient mongo;
//	private MongoCollection<Document> eventsCollection;
// 
//	private String host = "192.168.0.239"; // 地址
//	private int port = 27017;           // 端口号 
//	private String dbName = "auth";       // 库名
//	private String collectionName;      // 集合名
//	private String username;            // 用户名
//	private String password;            // 密码
//	protected String source;            
//    
//	private int connectionsPerHost = 10; // 空闲线程池中最大链接数 
//	private int threadsAllowedToBlockForConnectionMultiplier = 5; //一个线程等待链接可用的最大等待毫秒数
//	private int maxWaitTime = 1000 * 60 * 2;  // 最长等待时间
//	private int connectTimeout;
//	private int socketTimeout;
//	private int wtimeout;
// 
//	MongoDBAppenderBase(String collectionName) {
//		this.collectionName = collectionName;
//	}
// 
//	@Override
//	public void start() {
//		try {
//			connectToMongoDB();
//			super.start();
//		} catch (UnknownHostException e) {
//			addError( "Error connecting to MongoDB server: " + host + ":" + port,
//					e);
//		}
//	}
// 
//	private void connectToMongoDB() throws UnknownHostException {
//			// 用户名 数据库 密码
//			if (username != null && password != null){
//				MongoCredential credential = MongoCredential.createCredential(username, dbName, password.toCharArray());
//				mongo = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential), buildOptions());
//			}else{
//				mongo = new MongoClient(new ServerAddress(host, port),  buildOptions());
//			}
// 
//			MongoDatabase db = mongo.getDatabase(dbName);
//			eventsCollection = db.getCollection(collectionName);
// 
//	}
// 
//	private MongoClientOptions buildOptions() {
//		final MongoClientOptions.Builder options = new MongoClientOptions.Builder();
//		options.connectionsPerHost(connectionsPerHost) ;
//		options.threadsAllowedToBlockForConnectionMultiplier(threadsAllowedToBlockForConnectionMultiplier) ;
//		options.maxWaitTime(maxWaitTime) ;
//		options.connectTimeout(connectTimeout) ;
//		options.socketTimeout(socketTimeout) ;
//		options.maxWaitTime(wtimeout) ;
//		return options.build();
//	}
// 
//	protected abstract Document toMongoDocument(E event);
// 
//	@Override
//	protected void append(E eventObject) {
//		eventsCollection.insertOne(toMongoDocument(eventObject));
//	}
// 
//	@Override
//	public void stop() {
//		if (mongo != null)
//			mongo.close();
//		super.stop();
//	}
//    
//    //... 以下是变量get set 方法 此处省略
//}
